package com.example.anliku.service.serviceImpl;

import cn.hutool.json.JSONUtil;
import com.example.anliku.Utils.WeChatUtil;
import com.example.anliku.config.GlobalConfig;
import com.example.anliku.entity.Message;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.anliku.entity.User;
import com.example.anliku.mapper.UserMapper;
import com.example.anliku.service.UserService;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private GlobalConfig globalConfig;

    @Override
    public Message deleteByPrimaryKey(String userId) {
        return userMapper.deleteByPrimaryKey(userId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public Message insertSelective(User record) {
        Date nowdate = new Date();
        record.setUserTime(nowdate);
        User user = userMapper.selectByPrimaryKey(record.getUserId());
        if (user == null) {
            return userMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
        } else {
            return Message.fail("用户id重复");
        }
    }

    @Override
    public Message selectByPrimaryKey(String userId) {
        return Message.success(null).add(userMapper.selectByPrimaryKey(userId));
    }

    @Override
    public Message updateByPrimaryKeySelective(User record) {
        userMapper.updateByPrimaryKeySelective(record);
        return Message.success("修改成功").add(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message login(String userId, String userPwd) {
        User user = userMapper.selectAllByUserIdAndUserPwd(userId, userPwd);
        if (user == null) {
            return Message.fail("帐号密码错误");
        } else {
            if (user.getUserState() == 0) {
                return Message.success(null).add(user);
            } else if (user.getUserState() == -1) {
                return Message.fail("请等待管理员审核！");
            } else {
                return Message.fail("帐号已封禁！");
            }
        }
    }

    @Override
    public Message updateUserPwdByUserId(String userId, String userPwd) {
        return userMapper.updateUserPwdByUserId(userId, userPwd) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectALL() {
        return Message.success(null).add(userMapper.selectALL());
    }

    @Override
    public Message selectAllByUserPhone(String userPhone) {
        if (CollectionUtils.isNotEmpty(userMapper.selectAllByUserPhone(userPhone))) {
            return Message.fail("手机号已存在！");
        }
        return Message.success("手机号不存在！");
    }

    @Override
    public Message bindingOpenIdByUserId(String userId, String openId) {
        if(userMapper.selectByPrimaryKey(userId)!=null){//有这个用户
            return userMapper.updateUserOpenidByUserId(openId,userId) > 0 ? Message.success("openId绑定成功") : Message.fail(null);
        }else{
            return Message.fail("用户不存在无法绑定openId");
        }
    }

    @Override
    public Message cancelBindingOpenIdByUserId(String userId) {
        return userMapper.updateUserOpenidByUserId(null,userId) > 0 ? Message.success("openId解绑成功") : Message.fail(null);
    }

    @Override
    public Message wxLogin(String jsCode) {
        String rs = WeChatUtil.wechatLogin(jsCode, globalConfig.programAppId, globalConfig.programAppSecret);
        String openId = JSONUtil.parseObj(rs).getStr("openid");
        if (openId == null)
            return Message.fail("与微信官方服务器通信出错");
        User user = userMapper.selectByUserOpenid(openId);
        if (user != null) {
            return Message.success(null).add(user);
        } else {
            return Message.success(null).add(openId);
        }
    }

}

